<template>
  <div class="basicForm">
    <BasicForm @register="registerForm">
      <!-- 插槽（备注信息） -->
      <template #remarks="{ model, field }">
        <WangEditor
          v-model:value="model[field]"
          :bizKey="record.id"
          :bizType="'testDataChild_' + field"
          :height="300"
        />
      </template>
    </BasicForm>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsTestTestDataForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';

  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicTable, useTable } from '/@/components/Table';
  import { WecharCusInvPriceRoute } from '/@/api/setting/price/wecharCusInvPriceRoute';

  import { WangEditor } from '/@/components/WangEditor';
  import { formSchemas } from '/@/utils/custom';
  import { toFixed } from 'ant-design-vue/lib/input-number/src/utils/MiniDecimal';
  import { basWarehousetreeData } from '/@/api/bas/house/basWarehouse';

  const props = defineProps({
    schemas: { type: Array, default: [] },
    data: { type: Object, default: {} },
    soMainData: { type: Object, default: {} },
    formConfig: { type: Object, default: {} },
    rowType: { type: String, default: '' },
    cbustype: { type: String, default: '' },
    ope: { type: String, default: '' },
  });
  let basInvData = ref<any>({});
  let inputFormSchemas: FormSchema[] = [];
  watch(
    () => props.data,
    () => {
      inputFormSchemas = formSchemas(
        props.schemas,
        props.formConfig.formColNum,
        props.data,
      ) as FormSchema[];
      // 新增行
      inputFormSchemas = inputFormSchemas.map((item) => {
        if (item.field == 'cbdefine1' || item.field == 'cbdefine5') {
          item.defaultValue = 0;
        }
        if (item.field == 'basInv.cinvcode') {
          item.componentProps.params = {
            cusCode: props.soMainData.cusCode,
            croute: props.soMainData.croute,
          };
          item.componentProps.checkbox = false;

          item.componentProps.onSelect = (e) => {
            basInvData.value = e[0];
            setFieldsValue({
              soCode: props.data.soCode,
              cinvcode: e[0].cinvcode,
              basInv: {
                cinvname: e[0].cinvname,
                cinvcode: e[0].cinvcode,
              },
              cunitid: e[0].appUnitName,
              itaxunitprice: e[0].appPrice,
              idiscount: '0.00',
              cbdefine2: '0.00',
              iinvexchrate: e[0].ichangrate ? e[0].ichangrate : 1,
              itaxrate: e[0].itaxrate,
              cbdefine1: 0,
              historyPrice: basInvData.value.appPrice ? basInvData.value.appPrice : '0.00',
            });
          };
        }
        if (item.field == 'bodyVO.cdefine22') {
          item.componentProps.checkbox = false;
        }
        if (item.field == 'inum') {
          item.componentProps.onChange = (e) => {
            let params = {
              ...getFieldsValue(),
              iquantity: Math.round(
                Number(getFieldsValue().inum).toFixed(4) *
                  (getFieldsValue().iinvexchrate && getFieldsValue().iinvexchrate != '0.00'
                    ? Number(getFieldsValue().iinvexchrate)
                    : 1),
              ),
              isum: Number(
                Number(getFieldsValue().inum).toFixed(4) * Number(getFieldsValue().itaxunitprice),
              ).toFixed(2),
            };
            setFieldsValue(params);
          };
        }
        if (item.field == 'iquantity') {
          item.componentProps.onChange = (e) => {
            let inum: any = (
              Number(getFieldsValue().iquantity) /
              (getFieldsValue().iinvexchrate && getFieldsValue().iinvexchrate != '0.00'
                ? Number(getFieldsValue().iinvexchrate)
                : 1)
            ).toFixed(4);
            let params = {
              ...getFieldsValue(),
              // iquantity: Math.round(
              //   Number(getFieldsValue().inum).toFixed(4) *
              //     (getFieldsValue().iinvexchrate ? Number(getFieldsValue().iinvexchrate) : 1),
              // ),
              inum: inum,
              isum: Number(inum * Number(getFieldsValue().itaxunitprice)).toFixed(2),
            };
            setFieldsValue(params);
          };
        }

        if (item.field == 'cbdefine1') {
          item.componentProps.onChange = (e) => {
            let params = {
              ...getFieldsValue(),
              cbdefine2: getFieldsValue().cbdefine1 > 0 ? '0.00' : getFieldsValue().isum,
            };
            setFieldsValue(params);
          };
        }
        if (item.field == 'bgift') {
          item.componentProps.onChange = (e) => {
            if (e === '1') {
              let params = {
                ...getFieldsValue(),
                itaxunitprice: '0.00',
                isum: '0.00',
              };
              setFieldsValue(params);
            } else {
              let params = {
                ...getFieldsValue(),
                itaxunitprice: basInvData.value.appPrice
                  ? basInvData.value.appPrice
                  : props.data.historyPrice, // props.data.itaxunitprice,
                isum:
                  Number(
                    basInvData.value.appPrice ? basInvData.value.appPrice : props.data.historyPrice,
                  ) === 0
                    ? 0.0
                    : Number(getFieldsValue().inum) *
                      Number(
                        basInvData.value.appPrice
                          ? basInvData.value.appPrice
                          : props.data.historyPrice,
                      ),
              };
              setFieldsValue(params);
            }
          };
        }
        if (item.field == 'cmemo') {
          item = {
            label: item.label,
            field: item.field,
            component: item.component,
            componentProps: {
              dictType: item.componentProps.dictType,
              allowClear: item.componentProps.allowClear,
              mode: 'multiple',
            },
            itemProps: { validateTrigger: 'blur' },
          };
        }
        if (item.field === 'bodyVO.cdefine22') {
          item.componentProps = {
            api: basWarehousetreeData,
            params: { isAll: false, saBusType: props.cbustype || props.soMainData.cbustype },
            canSelectParent: false,
            allowClear: true,
          };
        }
        return item;
      });

      setTimeout(() => {
        zdy(props.data);
      }, 0);
    },
    { immediate: true },
  );

  const { t } = useI18n('test.testData');
  const record = ref<WecharCusInvPriceRoute>({} as WecharCusInvPriceRoute);
  const [
    registerForm,
    { resetFields, setFieldsValue, getFieldsValue, updateSchema, validate, setProps },
  ] = useForm();
  async function zdy(data) {
    record.value = data;
    // inputFormSchemas.forEach((item) => {
    //   if (item.field === 'cusCode') {
    //     item.dynamicDisabled = !record.value.isNewRecord;
    //   }

    //   if (item.field === 'invCode') {
    //     item.dynamicDisabled = !record.value.isNewRecord;
    //   }
    // });
    setProps({
      labelWidth: 120, // 控件前面的标签宽度
      schemas: inputFormSchemas, // 控件定义列表
      baseColProps: { lg: parseInt(props.formConfig.formColNum), md: 24 },
    });
    await resetFields(); // 重置表单数据
    if (data.itaxunitprice === 0) {
      data.itaxunitprice = '0.00';
    }
    await setFieldsValue(data); // 设置字段值
  }

  async function validateForm() {
    try {
      const data = await validate(); // 验证表单，并返回数据
      return data;
    } catch (error: any) {
      if (error && error.errorFields) {
        return false;
      }
    }
  }
  defineExpose({
    validateForm,
  });
</script>
<style>
  .col {
    color: red;
    font-weight: 100;
  }
</style>
